using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;

namespace WordMemorizer
{
    public partial class MainForm : Form
    {
        private const string DbPath = "words.db";
        private const string TableName = "WordsList";
        private List<WordItem> wordList = new List<WordItem>();
        private int currentIndex = 0;

        public MainForm()
        {
            InitializeComponent();
            InitializeDatabase();
            LoadWordsList();
            ShowCurrentWord();
        }

        // 单词数据类
        private class WordItem
        {
            public string English { get; set; }
            public string Chinese { get; set; }
        }

        // 初始化数据库
        private void InitializeDatabase()
        {
            if (!File.Exists(DbPath))
            {
                using (var conn = new SQLiteConnection($"Data Source={DbPath};Version=3;"))
                {
                    conn.Open();
                    var createTableCmd = conn.CreateCommand();
                    createTableCmd.CommandText = $@"
                        CREATE TABLE IF NOT EXISTS {TableName} (
                            Id INTEGER PRIMARY KEY AUTOINCREMENT,
                            English TEXT NOT NULL,
                            Chinese TEXT NOT NULL
                        )";
                    createTableCmd.ExecuteNonQuery();

                    // 插入测试数据
                    var insertCmd = conn.CreateCommand();
                    insertCmd.CommandText = $@"
                        INSERT INTO {TableName} (English, Chinese) VALUES
                        ('apple', '苹果'),
                        ('book', '书'),
                        ('cat', '猫'),
                        ('dog', '狗'),
                        ('elephant', '大象')";
                    insertCmd.ExecuteNonQuery();
                }
            }
        }

        // 加载单词列表
        private void LoadWordsList()
        {
            wordList.Clear();
            using (var conn = new SQLiteConnection($"Data Source={DbPath};Version=3;"))
            {
                conn.Open();
                var cmd = conn.CreateCommand();
                cmd.CommandText = $"SELECT English, Chinese FROM {TableName}";
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        wordList.Add(new WordItem
                        {
                            English = reader.GetString(0),
                            Chinese = reader.GetString(1)
                        });
                    }
                }
            }
        }

        // 显示当前单词
        private void ShowCurrentWord()
        {
            if (wordList.Count == 0)
            {
                chineseLabel.Text = "数据库中没有单词，请添加单词";
                return;
            }

            chineseLabel.Text = wordList[currentIndex].Chinese;
            inputTextBox.Text = "";
            resultLabel.Text = "";
            inputTextBox.Focus();
        }

        // 检查答案
        private void CheckAnswer()
        {
            if (wordList.Count == 0) return;

            string userInput = inputTextBox.Text.Trim().ToLower();
            string correctAnswer = wordList[currentIndex].English.ToLower();

            resultLabel.Text = userInput == correctAnswer ? "正确" : $"错误（正确答案：{correctAnswer}）";
            
            // 切换到下一个单词
            currentIndex = (currentIndex + 1) % wordList.Count;
            ShowCurrentWord();
        }

        // 窗体加载事件
        private void MainForm_Load(object sender, EventArgs e)
        {
            inputTextBox.KeyPress += InputTextBox_KeyPress;
        }

        // 输入框回车事件
        private void InputTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;
                CheckAnswer();
            }
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        // 界面组件（需在设计器中创建）
        private Label chineseLabel;
        private TextBox inputTextBox;
        private Label resultLabel;

        // 设计器生成的代码（需手动创建界面或通过Visual Studio设计器添加）
        private void InitializeComponent()
        {
            chineseLabel = new Label();
            inputTextBox = new TextBox();
            resultLabel = new Label();

            SuspendLayout();

            // 中文标签
            chineseLabel.AutoSize = true;
            chineseLabel.Location = new System.Drawing.Point(12, 25);
            chineseLabel.Size = new System.Drawing.Size(80, 15);
            chineseLabel.Text = "中文词义：";

            // 输入框
            inputTextBox.Location = new System.Drawing.Point(12, 50);
            inputTextBox.Size = new System.Drawing.Size(200, 23);

            // 结果标签
            resultLabel.AutoSize = true;
            resultLabel.Location = new System.Drawing.Point(12, 80);
            resultLabel.ForeColor = System.Drawing.Color.Red;

            // 窗体设置
            ClientSize = new System.Drawing.Size(240, 120);
            Controls.Add(resultLabel);
            Controls.Add(inputTextBox);
            Controls.Add(chineseLabel);
            Text = "背单词程序";
            ResumeLayout(false);
            PerformLayout();
        }
    }
}
